package com.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.beans.AdminInfo;
import com.common.Common.EnumAdminState;
import com.dao.AdminDao;
import com.utils.DESUtil;
import com.utils.IDUtil;

/**
 * 注册界面
 */
@WebServlet("/RegistrServlet.do")
public class RegistrServlet extends HttpServlet {
	private AdminDao adminDao=new AdminDao();
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String adminName=request.getParameter("adminName");
		String password=request.getParameter("password");
		String note=request.getParameter("note");
		StringBuffer msg=new StringBuffer();
		if("".equals(adminName)) {
			msg.append("#用户名不能为空#");
		}else if(!adminName.matches("^[0-9a-zA-Z]{4,15}$")){
			msg.append("#用户名格式不正确#");
		}
		if("".equals(password)) {
			msg.append("#密码不能为空#");
		}else if(!password.matches("^[0-9a-zA-Z]{6,20}$")){
			msg.append("#密码格式不正确#");
		}
		if(!"".equals(msg.toString())) {
			request.setAttribute("msg", msg.toString());
			request.getRequestDispatcher("/admin/admin_add.jsp").forward(request,response);
			return;
		}else {
			AdminInfo admin=new AdminInfo();
			admin.setAdminName(adminName);
			admin.setPassword(DESUtil.encrypt(password));
			admin.setNote(note);
			admin.setState(EnumAdminState.正常.getValue()+"");
			
			int result=adminDao.addAdmin(admin);
			if(result==1) {
				request.setAttribute("msg", "用户添加成功");
				request.getRequestDispatcher("/admin/admin_add.jsp").forward(request,response);
			}
		}
		
	}


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		doGet(request, response);
	}

}
